#include <project.h>
#include "userMain.h"

/* Defines second ROW from the last ROW */
#define CY_TEST_FLASH_ROW       (CY_FLASH_NUMBER_ROWS - 2u)

/* Defines absolut address of ROW */
#define CY_TEST_FLASH_ADDR      (CY_TEST_FLASH_ROW * CY_FLASH_SIZEOF_ROW)

uint8 userSaveData[CY_FLASH_SIZEOF_ROW];
uint8 userFlashReadData[5];
extern uint8 userEnableInfo;
extern uint8 userSensorConfig;
extern uint8 userDefaultConfig;
void userFlashSaveFunc(void)
{
    userNodeRegisterInfoUpdateFunc();
    if(userCompareDataFunc()==0) return ;
    userSaveData[0] = 0x01;
    userSaveData[1] = userEnableInfo;
    userSaveData[2] = userSensorConfig;
    userSaveData[3] = userDefaultConfig;
    CySysFlashWriteRow(CY_TEST_FLASH_ROW, userSaveData);
}
extern uint8 userMotionEnable;
extern uint8 userLightSensorEnable;
extern uint8 userBeepEnable;
extern uint8 userNightEnable;
extern uint8 userBeepVolumeLevel;
extern uint8 userNightLevel;
extern uint8 userLedLevel;
extern uint8 userMotionLevel;
extern uint8 userLightDefaultBindEnable;
extern uint8 userRelayDefaultBindEnable;
extern uint8 userLightSensorLevelDefault;		//the reference light sensor detect Level

uint8 userCloseDelay=1;
void userFlashReadFunc(void)
{
    uint8 userVar;
    for(userVar = 0;userVar<5;userVar++)
    {
        userFlashReadData[userVar] = (*((uint8 *) (CY_TEST_FLASH_ADDR + userVar)));
    }
    if(userFlashReadData[0]==1)
    {
        userEnableInfo = userFlashReadData[1];
        userSensorConfig = userFlashReadData[2];
        userDefaultConfig = userFlashReadData[3];
    }
    else
    {
        userEnableInfo = 0x0B;
        userSensorConfig = 0x3F;
        userDefaultConfig = 0x71;
        
    }

    if(userEnableInfo & 0x01)   userMotionEnable = 1;
     else   userMotionEnable = 0;
    if(userEnableInfo & 0x02)   userLightSensorEnable = 1;
     else   userLightSensorEnable = 0;
    if(userEnableInfo & 0x08)   userBeepEnable = 1;
     else   userBeepEnable = 0;
    //if(userEnableInfo & 0x10)   userNightEnable = 1;
    // else   userNightEnable = 0;
    
    if(userSensorConfig & 0x01)     userBeepVolumeLevel = 1;
     else       userBeepVolumeLevel = 0;
    if(userSensorConfig & 0x02)     userNightLevel = 1;
     else       userNightLevel = 0;
    if(userSensorConfig & 0x04)     userLedLevel = 1;
     else       userLedLevel = 0;
    if(userSensorConfig & 0x08)     userMotionLevel = 1;
    else        userMotionLevel = 0;
    if(userSensorConfig & 0x10)     userLightDefaultBindEnable = 1;
    else        userLightDefaultBindEnable = 0;
    if(userSensorConfig & 0x20)     userRelayDefaultBindEnable = 1;
    else        userRelayDefaultBindEnable = 0;
    
    userCloseDelay = userDefaultConfig & 0x0F;
    if(userCloseDelay==0) userCloseDelay = 1;
    if(userCloseDelay > 13) userCloseDelay = 13;
    userLightSensorLevelDefault = userDefaultConfig & 0xF0;
    userLightSensorLevelDefault >>= 4;
    if(userLightSensorLevelDefault == 0)  userLightSensorLevelDefault = 1;
    if(userLightSensorLevelDefault > 10) userLightSensorLevelDefault = 10;
    
}

uint8 userEnableInfoPre=0;
uint8 userSensorConfigPre=0;
uint8 userDefaultConfigPre=0;
uint8 userCompareDataFunc(void)
{
    uint8 userValue=0;
    if(userEnableInfo != userEnableInfoPre) 
    {
        userEnableInfoPre = userEnableInfo;
        userValue |= 0x01;
    }
    if(userSensorConfig != userSensorConfigPre)
    {
        userSensorConfigPre = userSensorConfig;
        userValue |= 0x02;
    }
    if(userDefaultConfig != userDefaultConfigPre)
    {
        userDefaultConfigPre = userDefaultConfig;
        userValue |= 0x04;
    }
    return userValue;
}

void userFactoryResetFunc(void)
{
    userEnableInfo = 0x0B;
    userSensorConfig = 0x3F;
    userDefaultConfig = 0x71;

    userMotionEnable = 1;
    userLightSensorEnable = 1;
    userBeepEnable = 1;
    
    userBeepVolumeLevel = 1;
    userNightLevel = 1;
    userLedLevel = 1;
    userMotionLevel = 1;
    userLightDefaultBindEnable = 0;
    userRelayDefaultBindEnable = 0;
    
    userCloseDelay = userDefaultConfig & 0x0F;
    userLightSensorLevelDefault = userDefaultConfig & 0xF0;
    userLightSensorLevelDefault >>= 4;
}


//uint32  iloMatchCounts = 4000;
void WDT_Start(void)
{
     /* Start the WDT */
    CySysWdtEnable();
    /* Starts the ILO accuracy/Trim measurement */
    CySysClkIloStartMeasurement();
    /*Write WDT match value */
    CySysWdtWriteMatch(4000);
}